<?php
/**
 * Description of LikeData
 *
 * @author Samsung
 */
class LikeData {
    
    public static function name($module){
        return implode('_', array(
            Yii::app()->user->id,
            strtolower($module),
            strtolower(__CLASS__),
        ));
    }
    
    public static function getData($module){
        if(Yii::app()->user->isGuest) return array();
        
        $name=self::name($module);
        
        if(isset(Yii::app()->session[$name])){
            return Yii::app()->session[$name];
        }else{
            $model=Like::model()->module($module)->userId(Yii::app()->user->id)->published()->findAll();
            return Yii::app()->session[$name]= empty($model)? array():
                CHtml::listData($model,
                    function($model){
                        return $model->moduleId;
                    },
                    function($model){
                        return $model->statusCode;
                    }
                );
        }
    }
    
    public static function add($module, $value){
        $data=self::getData($module);
        if(!isset($data[$value])){
            $data[$value]=Option::STATUS_ACTIVE;
            Yii::app()->session[self::name($module)]=$data;
        }
    }
    
    public static function delete($module, $value){
        $data=self::getData($module);
        if(isset($data[$value])){
            unset($data[$value]);
            Yii::app()->session[self::name($module)]=$data;
        }
    }
}